import axios from 'axios';
import qs from 'qs';

Vue.prototype.$qs = qs;
import ServerConfig from '../../public/serverConfig.json';
import Vue from 'vue';
import qiniu from 'qiniu-js';
import fi from 'element-ui/src/locale/lang/fi';
//单图
export const imgUploads = (token, domain, address,file) => {
    return new Promise((resolve, reject) => {
        var timenow = Date.parse(new Date());
        var key = address + localStorage.getItem('uid') + '_' + timenow + '_' + file.name;
        var qiniu = require('qiniu-js');
        var config = {
            region: qiniu.region.cnEast2,
            uptoken: token,
            domain: domain,     //配置好的七牛云域名  如   https://cdn.qniyun.com/
            unique_names: false
        };
        var putExtra = {
            fname: '',
            params: {},
            mimeType: [] || null
        };
        var observer = {
            next(res) {
                if (res.total.percent === 100) {
                    console.log(key);
                    resolve({
                        status:1,
                        file:key
                    });
                }
            },
            error(err) {
                resolve({
                    status:2,
                    msg:'上传失败'
                });
            },
            complete(res) {
            }
        };
        var observable = qiniu.upload(file, key
            , token, putExtra, config);
        // 上传
        var subscription = observable.subscribe(observer);
    });
};
//批量
export const imgUploads2 = async (token, domain, address, file) => {
    let arr = [];
    let file2 = Object.keys(file);
    for (let i = 0; i < file2.length; i++) {
        arr.push(await imgUploadssingle(token, domain, address, file[i]));
    }
    return arr;
};
export const imgUploadssingle = (token, domain, address, file) => {
    return new Promise((resolve, reject) => {
        var timenow = Date.parse(new Date());
        var key = address + localStorage.getItem('uid') + '_' + timenow + '_' + file.name;
        var qiniu = require('qiniu-js');
        var config = {
            region: qiniu.region.cnEast2,
            uptoken: token,
            domain: domain,     //配置好的七牛云域名  如   https://cdn.qniyun.com/
            unique_names: false
        };
        var putExtra = {
            fname: '',
            params: {},
            mimeType: [] || null
        };
        var observable = qiniu.upload(file, key
            , token, putExtra, config);
        var subscription = observable.subscribe({
            next(res) {
                if (res.total.percent === 100) {
                    resolve(key);
                }
            },  // 接收上传进度信息的回调函数
            error(err) {
                resolve(2);
            },  //上传错误后触发
            complete(res) {
            }  //停止当前文件上传
        }); // 上传开始
        //  this.form.picture = this.qiniuaddr + this.QiniuData.key;
    });


};

function add0(m) {
    return m < 10 ? '0' + m : m;
}

function format(shijianchuo) {
    var time = new Date(shijianchuo);
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    return y + '-' + add0(m) + '-' + add0(d);
}

